﻿using System;


using System.Text;
using IAM.Interface.Model.Interface;
using System.Xml;
using System.Reflection;

namespace IAM.Interface.Model
{
    /// <summary>
    /// 权限项列表
    /// </summary>
    public class ACItems : AbstractConvertObject,IConvertXml
        
    {
        public ACItems()
        {
            _acitems = new System.Collections.ArrayList();
        }
        public ACItems(Dimension dimension)
        {
            _dimension = dimension;
            _acitems = new System.Collections.ArrayList();
        }
        #region 属性
        private System.Collections.ArrayList _acitems=null;
        protected Dimension _dimension = null;
        /// <summary>
        /// 权限项列表 ACItem集合
        /// </summary>
        public System.Collections.ArrayList Items
        {
            get
            {
                return _acitems;
            }
            set
            {
                _acitems = value;
            }
        }
        public Dimension Dimension
        {
            get
            {
                return _dimension;
            }
        }
        /// <summary>
        /// 权限数量
        /// </summary>
        public int Count
        {
            get
            {
                return Items.Count;
            }
        }
        #endregion
        #region IConvertXml

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append("<ACItems");
                _xml.Append(" Count=\""+this.Count.ToString()+"\"");
                _xml.Append(" >");
                foreach (object item in Items)
                {
                    _xml.Append(((ACItem)item).XML);
                }
                return _xml.ToString();
            }


        }

        public string XMLFinis
        {
            get
            {
                return "</ACItems>";
            }
        }
        public string XML
        {
            get
            {
                return XMLBoday + XMLFinis;
            }
        }
        #endregion
        #region AbstractConvertObject
        public override object ConvertToObject()
        {
            foreach (XmlNode xn in this.CurrentXmlNode.ChildNodes)
            {
                ACItem acitem = new ACItem(this);
                acitem.CurrentXmlNode = xn;
                acitem = (ACItem)acitem.ConvertToObject();
                this.Items.Add(acitem);
            }
            return this;
        }
        #endregion
        #region 重写Equals
        /// <summary>
        /// 确定指定的 ACItems 对象是否等效于当前 ACItems
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            ACItems acs = null;
            try
            {
                acs = (ACItems)obj;
            }
            catch
            {
                return false;
            }
            bool result = true;
            if (this.Items.Count != acs.Items.Count)
            {
                result = false;
            }
            else
            {
                foreach (object oneofsource in this.Items)
                {
                    ACItem acisource = (ACItem)oneofsource;
                    result = false;

                    foreach (object oneofobj in acs.Items)
                    {
                        ACItem aciobj = (ACItem)oneofobj;
                        if (acisource.Equals(aciobj))
                        {
                            result = true;
                            break;
                        }
                    }
                    if (result == false)
                    {
                        break;
                    }
                }
            }

            return result;
        }

        #endregion
        #region 重写GetHashCode
        /// <summary>
        /// 哈希函数
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            int result = 1;
            PropertyInfo[] pi = this.GetType().GetProperties();
            foreach (PropertyInfo onofpi in pi)
            {
                result = result + onofpi.GetHashCode();
            }
            return result;
        }
        #endregion
    }
}
